package com.jzd1997;

import java.lang.reflect.Array;
import java.util.Arrays;

public class MyArray {
    int[] elements = new int[0];

    public void add(int val){
        add(elements.length,val);
    }

    public void add(int index,int val){
        if(index<0||index>elements.length){
            throw new RuntimeException();
        }
        int[] newArr = new int[elements.length+1];
        for(int i=0;i<elements.length;i++){
            if(i<index) {
                newArr[i] = elements[i];
            }else{
                newArr[i+1] = elements[i];
            }
        }
        newArr[index] = val;
        elements = newArr;
    }

    public void remove(int index){
        int[] newArr = new int[elements.length-1];
        for(int i=0;i<newArr.length;i++){
            if(i<index){
                newArr[i] = elements[i];
            }else{
                newArr[i] = elements[i+1];
            }
        }
        elements = newArr;
    }

    public int get(int index){
        return elements[index];
    }

    public static void main(String[] args){
        MyArray myArray = new MyArray();
        myArray.add(8);
        myArray.add(4);
        myArray.add(6);
        myArray.add(2);
        System.out.println(Arrays.toString(myArray.elements));
        myArray.add(1,5);
        System.out.println(Arrays.toString(myArray.elements));
        myArray.remove(3);
        System.out.println(Arrays.toString(myArray.elements));
    }
}
